<template>
  <div class="application-list">
    <el-card class="box-card">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item label="申请状态" prop="status">
            <el-select v-model="searchForm.status" placeholder="请选择申请状态" clearable>
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <el-table :data="tableData" v-loading="loading" border style="width: 100%">
        <el-table-column prop="applicationDate" label="申请时间" width="180">
          <template slot-scope="scope">
            {{ formatDate(scope.row.applicationDate) }}
          </template>
        </el-table-column>
        <el-table-column label="职位名称">
          <template slot-scope="scope">
            <span v-if="scope.row.jobInfo">{{ scope.row.jobInfo.jobName }}</span>
            <span v-else>
              <el-button type="text" @click="getJobInfo(scope.row)">加载职位信息</el-button>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="求职者">
          <template slot-scope="scope">
            <span v-if="scope.row.seekerInfo">{{ scope.row.seekerInfo.name }}</span>
            <span v-else>
              <el-button type="text" @click="getSeekerInfo(scope.row)">加载求职者信息</el-button>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="用人单位">
          <template slot-scope="scope">
            <span v-if="scope.row.employerInfo">{{ scope.row.employerInfo.employerName }}</span>
            <span v-else>
              <el-button type="text" @click="getEmployerInfo(scope.row)">加载单位信息</el-button>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="申请状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="处理时间" width="180">
          <template slot-scope="scope">
            {{ formatDateTime(scope.row.updateTime) }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'AdminApplicationList',
  data() {
    return {
      loading: false,
      searchForm: {
        status: null
      },
      statusOptions: [
        { value: 0, label: '未处理' },
        { value: 1, label: '已聘用' },
        { value: 2, label: '已拒绝' }
      ],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.loading = true
      try {
        const response = await this.$http.post('/job-application/page', {
          current: this.currentPage,
          size: this.pageSize,
          ...this.searchForm
        })
        if (response.data.code === 200) {
          // 初始化数据,为每条记录添加信息字段
          this.tableData = response.data.data.records.map(item => ({
            ...item,
            jobInfo: null,
            seekerInfo: null,
            employerInfo: null
          }))
          this.total = response.data.data.total
          
          // 获取每条记录的详细信息
          this.tableData.forEach(item => {
            this.getJobInfo(item)
            this.getSeekerInfo(item)
            this.getEmployerInfo(item)
          })
        } else {
          this.$message.error(response.data.msg || '获取申请列表失败')
        }
      } catch (error) {
        console.error('获取申请列表失败:', error)
        this.$message.error('获取申请列表失败')
      } finally {
        this.loading = false
      }
    },
    
    // 获取职位信息 - 修改路径
    async getJobInfo(row) {
      try {
        const response = await this.$http.get(`/job/${row.jobId}`)
        if (response.data.code === 200) {
          this.$set(row, 'jobInfo', response.data.data)
        }
      } catch (error) {
        console.error('获取职位信息失败:', error)
      }
    },
    
    // 获取求职者信息 - 修改路径
    async getSeekerInfo(row) {
      try {
        const response = await this.$http.get(`/job-seeker/info/${row.seekerId}`)
        if (response.data.code === 200) {
          this.$set(row, 'seekerInfo', response.data.data)
        }
      } catch (error) {
        console.error('获取求职者信息失败:', error)
      }
    },
    
    // 获取用人单位信息 - 修改路径
    async getEmployerInfo(row) {
      try {
        const response = await this.$http.get(`/employer/${row.employerId}`)
        if (response.data.code === 200) {
          this.$set(row, 'employerInfo', response.data.data)
        }
      } catch (error) {
        console.error('获取用人单位信息失败:', error)
      }
    },

    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    resetSearch() {
      this.$refs.searchForm.resetFields()
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    getStatusType(status) {
      const statusMap = {
        0: 'info',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status] || 'info'
    },
    getStatusText(status) {
      const statusMap = {
        0: '未处理',
        1: '已聘用',
        2: '已拒绝'
      }
      return statusMap[status] || '未知'
    },
    formatDateTime(time) {
      if (!time) return ''
      const date = new Date(time)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
    },
    formatDate(date) {
      if (!date) return ''
      return date.split(' ')[0]
    }
  }
}
</script>

<style scoped>
.application-list {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 